import {Http} from '../../../library';

export default class Restful {

    /**
     * 通用url前缀
     */
    static url = 'https://restful.sgboke.com/he_fuping/worker/';

    /**
     * 固定header
     */
    static header = {
        // 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
        'Accept': 'application/json',
        'Authorization': JSON.stringify({
            token: "64b47881c5aa34a5", 
            arrive: "testarrive",
            platform: "android",
            appname: "fuping",
            version: "v0.0.1"
        })
    };

    /**
     * 统一错误处理对象
     */
    static error = {
        code(res, obj) {
            console.log('=============  ' + obj.method + ' : ' + obj.url);
            console.log('Code失败!');
            console.log(res);
        },
        server(res, obj) {
            console.log('=============  ' + obj.method + ' : ' + obj.url);
            console.log('server失败!');
            console.log(res);
        },
        net(err, obj) {
            console.log('=============  ' + obj.method + ' : ' + obj.url);
            console.log('network失败!');
            console.log(err);
        }
    }

    /**
     * 统一成功处理函数
     */
    static success = (res, obj)=> {
        console.log('=============  ' + obj.method + ' : ' + obj.url);
        console.log('成功!');
        console.log(res);
    }

    /**
     * get请求
     * 
     * @param {String} url 请求地址
     */
    static get(url, data) {
        Http.get({
            url: this.url + url,
            data: data,
            header: this.header,
			error: this.error,
			success: this.success
		});
    }

    /**
     * post请求
     * 
     * @param {String} url 请求地址
     * @param {Object} data 请求的数据
     */
    static post(url, data) {
        Http.post({
            url: this.url + url,
            header: this.header,
            data: data,
			error: this.error,
			success: this.success
		});
    }
}
